<template>
  <div class="app-container">
    <!-- 头部 -->
    <div class="ProductionDynamicHeader">
      <Find :OriginalFilter="{ static: true }" @findClicked="findData" :formList="formList" @Launchform="Launchform" />
    </div>

    <el-card style="margin-top: 15px" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 100%">
          <span style="font-weight: 600">产前计划动态</span>
          <span style="font-weight: 600; color: red"
            >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ps:修改日期需要的格式:例如2023-05-02/双位数日期例如:2023-11-12</span
          >
          <Table
            :isLoading="isLoading"
            @inputChange="confirmChange5"
            class="table1"
            :tableHeader="tableHeader"
            :tableData="PrenatalPlanningInquiryTableDateList"
            :TableHeight="100"
            :outTableHeight="75"
          />
        </div>
      </div>

      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'PrenatalPlanning'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { PrenatalPlanningInquiry, updateLotNo } from '@/api/Control/FactoryControl/PrenatalPlanning'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'

const isLoading = ref(true)

// 产前计划动态
const bussActArrList = ref<any>({
  bussAct: '',
  lotNo: '',
  packMtlAct: '',
  soleMtlPmc: '',
  soleMtlVendor: '',
  solePurAct: '',
  techBomAct: '',
  techTrailAct: '',
  upperMtlAct: '',
  upperMtlPmc: '',
  upperMtlVendor: '',
  upperPurAct: ''
})

const confirmChange5 = async (row?: any) => {
  bussActArrList.value.bussAct = row.bussAct
  bussActArrList.value.lotNo = row.lotNo
  bussActArrList.value.packMtlAct = row.packMtlAct
  bussActArrList.value.soleMtlPmc = row.soleMtlPmc
  bussActArrList.value.soleMtlVendor = row.soleMtlVendor
  bussActArrList.value.solePurAct = row.solePurAct
  bussActArrList.value.techBomAct = row.techBomAct
  bussActArrList.value.techTrailAct = row.techTrailAct
  bussActArrList.value.upperMtlAct = row.upperMtlAct
  bussActArrList.value.upperMtlPmc = row.upperMtlPmc
  bussActArrList.value.upperMtlVendor = row.upperMtlVendor
  bussActArrList.value.upperPurAct = row.upperPurAct

  console.log(bussActArrList.value)

  const re = /^(\d{4})-(\d{2})-(\d{2})$/
  let active = true

  // 遍历每个属性，检查日期格式
  Object.keys(bussActArrList.value).forEach((key: string) => {
    if (key !== 'lotNo') {
      const dateValue = bussActArrList.value[key]
      if (dateValue !== null && !re.test(dateValue) && dateValue !== '') {
        active = false
      }
    }
  })

  if (!active) {
    JMessage.warning('日期格式错误应该为: xxxx-xx-xx')
  } else {
    try {
      const result = await LoginWork.put<any, any>('/bioDeptWrok/befPrepairTimeUp', bussActArrList.value)
      if (result.code === 200) {
        PrenatalPlanningInquiryMethods()
        JMessage.success(result.message)
      } else {
        JMessage.error(result.message)
      }
    } catch (error) {
      console.error('An error occurred while updating bussActArrList:', error)
      JMessage.error('更新数据时发生错误')
    }
  }
}

const LotNoList = ref(0)

const updateLotNoMethods = async () => {
  const result = await updateLotNo(LotNoList.value)
  if (result.code === 200) {
    PrenatalPlanningInquiryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const clearFormList2 = () => {
  formList.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    } else if (item.type === 'select') {
      item.selectValue = ''
    } else if (item.type === 'type') {
      item.dateValue = ''
    }
  })
}

const findData = async (count: string) => {
  switch (count) {
    case '查询采购周期不足的订单': {
      PrenatalPlanningInquiryParms.value.flag = 1
      PrenatalPlanningInquiryMethods()
      break
    }
    case '查询物料交期不满足生产需求的订单': {
      PrenatalPlanningInquiryParms.value.flag = 2
      PrenatalPlanningInquiryMethods()
      break
    }
    case '已延误明细预警': {
      if (PrenatalPlanningInquiryParms.value.flow === '') {
        JMessage.warning('请先选择作业流程在进行查询')
      } else {
        PrenatalPlanningInquiryParms.value.flag = 3
        PrenatalPlanningInquiryMethods()
      }
      break
    }
    case '三天内待完成预警': {
      if (PrenatalPlanningInquiryParms.value.flow === '') {
        JMessage.warning('请先选择作业流程在进行查询')
      } else {
        PrenatalPlanningInquiryParms.value.flag = 4
        PrenatalPlanningInquiryMethods()
      }
      break
    }
    case '更新指令': {
      LotNoList.value = 1
      updateLotNoMethods()
      break
    }
    case '更新资料': {
      LotNoList.value = 2
      updateLotNoMethods()
      break
    }
    case '置空': {
      clearFormList2()
      break
    }
    case '导出查询资料': {
      const res = await fileRequest.put<any, any>(`/bioDeptWrok/getProdBefExpord?flag=${2}`, PrenatalPlanningInquiryParms.value)
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      link.download = `产前计划安排查询寻资料${Date.now()}.xls` //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)

      break
    }
    case '导出产前追踪表': {
      const res = await fileRequest.put<any, any>(`/bioDeptWrok/getProdBefExpord?flag=${1}`, PrenatalPlanningInquiryParms.value)
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      link.download = `产前计划安排产前追踪表${Date.now()}.xls` //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)

      break
    }
  }
}

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectOptions: [],
    selectP: '品牌'
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectOptions: [],
    selectP: '型体'
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectOptions: [],
    selectP: '楦头'
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },

  {
    type: 'date',
    dateLabel: '客人交期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '更新指令'
  },
  {
    type: 'button',
    checkboxLabel: '更新资料'
  },
  {
    type: 'button',
    checkboxLabel: '导出查询资料'
  },
  {
    type: 'button',
    checkboxLabel: '导出产前追踪表'
  },
  {
    type: 'button',
    checkboxLabel: '查询采购周期不足的订单'
  },
  {
    type: 'button',
    checkboxLabel: '查询物料交期不满足生产需求的订单'
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectOptions: [],
    selectP: '作业流程'
  },
  {
    type: 'button',
    checkboxLabel: '已延误明细预警'
  },
  {
    type: 'button',
    checkboxLabel: '三天内待完成预警'
  },
  {
    type: 'button',
    checkboxLabel: '导入需求日期'
  }
])

const EvaluatesTheArrayReturnValue3 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const Launchform = (row: any) => {
  PrenatalPlanningInquiryParms.value.lotNo = row[0].inputValue
  PrenatalPlanningInquiryParms.value.custom = row[1].selectValue
  PrenatalPlanningInquiryParms.value.style = row[2].selectValue
  PrenatalPlanningInquiryParms.value.lastNo = row[3].selectValue

  PrenatalPlanningInquiryParms.value.orderDateStart = row[4].dateValue[0]
  PrenatalPlanningInquiryParms.value.orderDateEnd = row[4].dateValue[1]

  PrenatalPlanningInquiryParms.value.deliveryDateStart = row[5].dateValue[0]
  PrenatalPlanningInquiryParms.value.deliveryDateEnd = row[5].dateValue[1]
  PrenatalPlanningInquiryParms.value.flow = row[13].selectValue
  PrenatalPlanningInquiryMethods()
}

// 品牌获取
const lastsQuiryMethods = async () => {
  const result = await service.get<any, any>(`/bioDeptWrok/getBefLast`)
  console.log(EvaluatesTheArrayReturnValue3(result))
  formList.value[3].selectOptions = EvaluatesTheArrayReturnValue3(result.filter((item: any) => item !== null))
}

// 品牌获取
const AddBrandDateMethods = async () => {
  const result = await service.get<any, any>(`/bioDeptWrok/getBefCustom`)
  formList.value[1].selectOptions = EvaluatesTheArrayReturnValue3(result)
}

// 型体获取
const AddBrandDateMethodsList = async () => {
  const result = await service.get<any, any>(`/bioDeptWrok/getBefStyle`)
  formList.value[2].selectOptions = EvaluatesTheArrayReturnValue3(result)
}

// 作业流程
const workFlow = async () => {
  const result = await service.get<any, any>(`/bioDeptWrok/getBefPrepair`)
  formList.value[13].selectOptions = EvaluatesTheArrayReturnValue3(result)
}

// 产前计划安排查询
const PrenatalPlanningInquiryParms = ref({
  custom: '',
  deliveryDateEnd: '',
  deliveryDateStart: '',
  flag: 0,
  flow: '',
  lastNo: '',
  lotNo: '',
  orderDateEnd: '',
  orderDateStart: '',
  pageNum: 1,
  pageSize: 40,
  style: ''
})

const handleSizeChange = (val: number) => {
  PrenatalPlanningInquiryParms.value.pageSize = val
  PrenatalPlanningInquiryMethods()
}
const handleCurrentChange = (val: number) => {
  PrenatalPlanningInquiryParms.value.pageNum = val
  PrenatalPlanningInquiryMethods()
}

const total = ref()

const PrenatalPlanningInquiryTableDateList = ref([])

const PrenatalPlanningInquiryMethods = async () => {
  isLoading.value = true
  const result = await PrenatalPlanningInquiry(PrenatalPlanningInquiryParms.value)
  PrenatalPlanningInquiryTableDateList.value = result.dtos.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })
  total.value = result.total
  isLoading.value = false
}

onMounted(() => {
  PrenatalPlanningInquiryMethods()

  AddBrandDateMethods()

  lastsQuiryMethods()

  AddBrandDateMethodsList()

  workFlow()
})

// 表格数据
const tableHeader = ref([
  {
    label: '指令',
    prop: 'lotNo',
    change: true
  },
  {
    label: '品牌',
    prop: 'custom',
    change: true
  },
  {
    label: '型体',
    prop: 'style',
    change: true
  },
  {
    label: '颜色',
    prop: 'color',
    change: true
  },
  {
    label: '订单数',
    prop: 'orderQty',
    change: true
  },
  {
    label: '楦头',
    prop: 'lastNo',
    change: true
  },
  {
    label: '下单日期',
    prop: 'orderDate',
    change: true
  },
  {
    label: '客人交期',
    prop: 'deliveryDate',
    change: true
  },
  {
    label: '成型线别',
    prop: 'lastLine',
    change: true
  },
  {
    label: '成型上线日',
    prop: 'lastDate',
    change: true
  },
  {
    Multistage: true,
    label: '业务色卡',
    children: [
      {
        label: '计划',
        prop: 'bussPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'bussAct'
      }
    ]
  },
  {
    Multistage: true,
    label: '技转核料',
    children: [
      {
        label: '计划',
        prop: 'techBomPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'techBomAct'
      }
    ]
  },
  {
    Multistage: true,
    label: '技转试做',
    children: [
      {
        label: '计划',
        prop: 'techTrailPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'techTrailAct'
      }
    ]
  },
  {
    Multistage: true,
    label: '面料下单',
    children: [
      {
        label: '计划',
        prop: 'upperPurPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'upperPurAct'
      }
    ]
  },
  {
    Multistage: true,
    label: '大底下单',
    children: [
      {
        label: '计划',
        prop: 'solePurPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'solePurAct'
      }
    ]
  },

  {
    Multistage: true,
    label: '主面料',
    children: [
      {
        label: '采购周期',
        prop: 'upperMtlLt',
        change: true
      },
      {
        label: '评审计划',
        prop: 'upperMtlPush',
        change: true
      },
      {
        label: '采购回复',
        prop: 'upperMtlVendor',
        change: true
      },
      {
        label: '生产需求',
        prop: 'upperMtlPull',
        change: true
      },
      {
        label: '差异天数',
        prop: 'upperMtlDiff',
        change: true
      },
      {
        label: '生控计划',
        prop: 'upperMtlPmc'
      },
      {
        label: '实际完成',
        prop: 'upperMtlAct'
      }
    ]
  },

  {
    Multistage: true,
    label: '大底',
    children: [
      {
        label: '采购周期',
        prop: 'soleMtlLt',
        change: true
      },
      {
        label: '评审计划',
        prop: 'soleMtlPush',
        change: true
      },
      {
        label: '采购回复',
        prop: 'soleMtlVendor',
        change: true
      },
      {
        label: '生产需求',
        prop: 'soleMtlPull',
        change: true
      },
      {
        label: '差异天数',
        prop: 'soleMtlDiff',
        change: true
      },
      {
        label: '生控计划',
        prop: 'soleMtlPmc'
      },
      {
        label: '实际完成',
        prop: 'soleMtlAct'
      }
    ]
  },
  {
    Multistage: true,
    label: '面衬',
    children: [
      {
        label: '计划',
        prop: 'sockMtlPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'sockMtlAct',
        change: true
      }
    ]
  },
  {
    Multistage: true,
    label: '包材',
    children: [
      {
        label: '计划',
        prop: 'packMtlPlan',
        change: true
      },
      {
        label: '实际',
        prop: 'packMtlAct'
      }
    ]
  },
  {
    Multistage: true,
    label: '外协工艺',
    color: 'red',
    children: [
      {
        label: '评审门槛',
        prop: 'hfLt',
        change: true
      },
      {
        label: '实际门槛',
        prop: 'hfAct',
        change: true
      },
      {
        label: '差异天数',
        prop: 'hfDiff',
        change: true
      }
    ]
  }
])
</script>

<style lang="less" scoped>
:deep(.ProductionDynamicMainCardBottom .el-input__wrapper) {
  background-color: #fff5eb;
}

.save {
  color: black;
  border: 1px solid #e7e7e7;
  background: #e7e7e7;
}

.table1 {
  :deep(th:nth-child(12)) {
    color: blue !important;
  }

  :deep(th:nth-child(11)) {
    color: blue !important;
  }

  :deep(th:nth-child(13)) {
    color: blue !important;
  }

  :deep(th:nth-child(14)) {
    color: blue !important;
  }

  :deep(th:nth-child(15)) {
    color: blue !important;
  }

  :deep(th:nth-child(16)) {
    color: red !important;
  }

  :deep(th:nth-child(17)) {
    color: red !important;
  }

  :deep(th:nth-child(18)) {
    color: blue !important;
  }

  :deep(th:nth-child(19)) {
    color: blue !important;
  }

  :deep(th:nth-child(20)) {
    color: red !important;
  }

  :deep(.el-table_1_column_26_column_27.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_26_column_28.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_26_column_29.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_26_column_30.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_26_column_31.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_26_column_32.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_26_column_33.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_34_column_35.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_34_column_36.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }

  :deep(.el-table_1_column_34_column_37.is-center.is-leaf.el-table__cell) {
    color: black !important;
  }
}
</style>
